<?php
/**
 * controller file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2020/05/11
 * Time: 16:24
 */

namespace Modules\Admin\Http\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Request;
use Modules\Admin\Service\HandbookService;
use Modules\Admin\Service\UpkeepTypeService;

class HandbookController extends BaseController
{
    /**
     *  保养手册
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function index()
    {
        $res = HandbookService::get_list();
        echo_error($res);

        return view('admin::handbook.index', $res['data']);
    }

    /**
     * 添加 XXX 页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function create()
    {
        $res = UpkeepTypeService::get_all();

        return view('admin::handbook.create', $res);
    }

    /**
     * 添加 操作
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @throws \Exception
     */
    public function store()
    {
        $arr_input = Request::input();

        shiwu_start();
        $res = HandbookService::add_one($arr_input);
        shiwu_end($res);

        return $res;
    }

    /**
     * 修改 XXX 页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit()
    {
        $nId = Request::get('id', 0);
        $book = UpkeepTypeService::get_all();
        $res = HandbookService::get_one((int)$nId);
        echo_error($res);

        $arr_data = [
            'arr_data' => $res['data'],
            'data' => $book['data']
        ];

        return view('admin::handbook.edit', $arr_data);
    }

    /**
     * 修改 XXX 页面操作
     * @return array|\Illuminate\Http\RedirectResponse
     * @throws \Exception
     */
    public function update()
    {
        $arr_input = Request::input();

        shiwu_start();
        $res = HandbookService::edit_one($arr_input);
        shiwu_end($res);

        return $res;
    }


    /**
     * 更改 XXX 状态
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_state()
    {
        $arr_input = Request::input();
        return HandbookService::change_one_state($arr_input);
    }

    /**
     * 删除 XXX
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete()
    {
        $n_id = (int)Request::get('id', 0);

        return HandbookService::delete_one($n_id);
    }
}